package com.xm.servlet.reception_desk.shopping_cart;

import com.xm.service.reception_desk.CommoditySpecificationService;
import com.xm.service.reception_desk.ShoppingCartService;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 新增一个购物车商品
 *
 * @author 康文程
 * @date 2024-1-4 下午 02:48
 */
@WebServlet("/add/shopping/cart")
public class AddShoppingCartServlet extends HttpServlet {
    /**
     * 购物车Service
     */
    private final ShoppingCartService service = new ShoppingCartService();
    /**
     * 商品Service
     */
    private final CommoditySpecificationService commoditySpecificationService = new CommoditySpecificationService();

    /**
     * 添加购物车商品
     *
     * @param req  an {@link HttpServletRequest} object that
     *             contains the request the client has made
     *             of the servlet
     * @param resp an {@link HttpServletResponse} object that
     *             contains the response the servlet sends
     *             to the client
     */
    @SneakyThrows(Exception.class)
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        String informationId = req.getParameter("informationId");
        final String isNull = "";
        //判断是否登陆
        if (!informationId.equals(isNull)){
            String shoppingCartId = req.getParameter("shoppingCartId");
            String commodityId = req.getParameter("commodityId");
            String specificationsByCommodityId = commoditySpecificationService.getSpecificationsByCommodityId(
                    Integer.parseInt(commodityId));
            service.addToShoppingCart(Integer.parseInt(informationId),
                    Integer.parseInt(shoppingCartId),specificationsByCommodityId,null);
            resp.getWriter().println(true);
        }else{
            resp.getWriter().println(false);
        }

    }
}
